import json

import matplotlib.pyplot as plt
import pandas as pd

# 显示中文标签
plt.rcParams["font.sans-serif"] = "SimHei"
# 显示负号
plt.rcParams["axes.unicode_minus"] = False


# 官方文档 https://www.pypandas.cn/docs/user_guide/io.html

def read_user():
    df = pd.read_excel("C:\\Users\\yl2028\\Desktop\\ybrain\\enterprise_20191101-20200505.xlsx")
    print(df)
    print(df.iloc[0])
    print(df["管理员名称"] +" "+ df["会议总时长"])
    # print(df.describe())


def create_df():
    df = pd.DataFrame(["1", "2"], ["a", "b"], ["c", "d"])
    print(df)

def read_agent_orders():
    pass

def read_ee_orders():
    pass

def read_vcs_project():
    vcs_project = json.load(open("C:\\Users\\yl2028\\Desktop\\ybrain\\数据预处理\\ymall_crm_vcs_project.json", encoding="utf-8"))
    project_list = vcs_project["RECORDS"]
    project_df = pd.DataFrame(project_list)
    print("project_dataframe")
    print(project_df)
    # 所有项目
    print(project_df.iloc[0])
    # 省报备项目排行榜
    print("-----province top10------")
    province_counts = sort_count(project_df,"province")
    print(province_counts)
    # 市项目排行榜
    city_counts = sort_count(project_df, "city")
    print("-----city top10------")
    print(city_counts)
    # 报备人员排行榜
    manager_counts = sort_count(project_df, "manager_name", 10)
    print("-----manager top10------")
    print(manager_counts)
    # 通过的报备项目
    manager_counts = sort_count(project_df, "project_status", 10)
    print("-----manager top10------")
    print(manager_counts)
    # 绘图
    for x, y in zip(province_counts.keys(), province_counts.values):
        plt.text(x, y + 2, "%.0f" % y, ha="center", va="bottom", fontsize=12)
    plt.bar(province_counts.keys(), province_counts.values, fc="r", tick_label=province_counts.keys())
    plt.xlabel("省")
    plt.ylabel("项目数量")
    plt.title("各省报备项目数")
    plt.show()


def sort_count(df, field, count=10):
    return df[field].groupby(df[field]).size().sort_values(ascending=False)[:count]


if __name__ == '__main__':
    # read_user()
    read_vcs_project()
    # create_df()
